package cn.cocho.custom.yunCashierDf.phone.service;

import cn.cocho.common.web.app.JsonResultContants;
import cn.cocho.common.web.character.DesEncrypt;
import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.common.web.utils.JsonUtils;
import cn.cocho.custom.yunCashierDf.agency.domain.AgencyInfo;
import cn.cocho.custom.yunCashierDf.cache.CacheCtrl;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantInfo;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantTransactionInfo;
import cn.cocho.custom.yunCashierDf.uc.domain.UcUserInfo;
import cn.cocho.custom.yunCashierDf.utils.AppConstants;
import cn.cocho.custom.yunCashierDf.utils.JsonResult;
import cn.cocho.custom.yunCashierDf.utils.Page;
import cn.cocho.dborm.core.Dborm;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by lp on 2017-05-24.
 */
@Service("phoneService")
public class PhoneService {

    @Autowired
    CacheCtrl cacheCtrl;

    public UcUserInfo login(String username, String userPassword) {
        UcUserInfo ucUserInfo = new UcUserInfo();
        ucUserInfo.setUsername(username);
        ucUserInfo.setUserPassword(DesEncrypt.encrypt(userPassword, AppConstants.PASSWORD_DES));
        return Dborm.getEntityByExample(ucUserInfo, true);
    }
    public JsonResult userInfoLogin(String username, String userPassword) {
        JsonResult result;
        UcUserInfo ucUserInfo = login(username, userPassword);
        if (ucUserInfo == null) {
            result = new JsonResult(JsonResultContants.FAIL, "手机号或密码错误！");
        } else if (ucUserInfo.getStatus().equals("STATUS_FORBIDDEN") || ucUserInfo.getStatus().equals("STATUS_FREEZE")) {
            result = new JsonResult(JsonResultContants.FAIL, "用户被冻结或禁用！");
        } else if (ucUserInfo.getStatus().equals("STATUS_NORMOR")) {
            String token = cacheCtrl.getTokenCtrl().addToken(ucUserInfo);
            result = new JsonResult();
            result.setStatusCode(JsonResultContants.SUCCESS);
            result.setStatusMsg("登录成功！");
            result.setToken(token);
            result.setContent(JsonUtils.toJSONString(ucUserInfo));
        } else {
            result = new JsonResult(JsonResultContants.FAIL, "登录失败！");
        }
        return result;
    }
    public UcUserInfo getUserInfo() {
        return cacheCtrl.getTokenCtrl().getCurrentUcUserInfo();
    }
    public Page<MerchantTransactionInfo> getMerchantTransactionInfoList(Page page,String terminalid ,String order,Date startTime,Date endTime) {

        if (page == null) {
            page = new Page();
            page.setRows(5);
        }
        String strWhere = " where 1 = 1 and terminalid = '"+terminalid+"'";
        if(startTime!=null && endTime!=null){
            strWhere+="   and  create_time BETWEEN  ";
            strWhere+= startTime.getTime();
            strWhere+="   and   ";
            strWhere+= endTime.getTime();
        }
        System.out.println(strWhere);

        //StringBuilder sqlData = new StringBuilder("select * from merchant_transaction_info "+strWhere+"");
        //StringBuilder sqlCount = new StringBuilder("select count(*) from merchant_transaction_info "+strWhere+" ");
        StringBuilder sqlData = new StringBuilder("select * from viewmerchantorder "+strWhere+"");
        StringBuilder sqlCount = new StringBuilder("select count(*) from viewmerchantorder "+strWhere+" ");
        if (StringUtils.isNotBlank(order)) {
            sqlData.append(" order by ");
            sqlData.append(order);
        } else {
            sqlData.append(" order by last_modify_time desc ");
        }
        sqlData.append(" limit ");
        sqlData.append(page.getRows());
        sqlData.append(" offset ");
        sqlData.append(page.getPageFirst());
        List<MerchantTransactionInfo> list = Dborm.getEntities(sqlData.toString(), null, MerchantTransactionInfo.class);
        page.setDataRows(list);
        Integer count = Dborm.getCount(sqlCount.toString(), null);
        if (count != null) {
            page.setRecordsCount(count);
        }
        return page;
    }
    public MerchantTransactionInfo getMTInfo(String ordernum,String terminalid){
        System.out.println(ordernum+"|Phone|"+terminalid);
        MerchantTransactionInfo example= new MerchantTransactionInfo();
        example.setTerminalid(terminalid);
        example.setOrigOrderNum(ordernum);
        return Dborm.getEntityByExample(example);

    }
    public MerchantInfo getMerchanInfo(String txamt,String ordernum){
        System.out.println(ordernum + "xxx" +txamt);
        String sql="select t2.* from merchant_transaction_info t LEFT JOIN merchant_info t2 on (t.merchant_id=t2.merchant_id) where t.txamt=?  and t.orig_order_num=?";
        String[] obj={txamt,ordernum};
        List<MerchantInfo> merchantInfolist = Dborm.getEntities(sql,obj,MerchantInfo.class);
        if(merchantInfolist.size()>0){
            return merchantInfolist.get(0);
        }else {
            return null;
        }
    }
}
